package com.food.servlet;

import com.food.dao.TaskDAO;
import com.food.model.Task;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/TaskServlet")
public class TaskServlet extends HttpServlet {
    private TaskDAO taskDAO = new TaskDAO();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String search = request.getParameter("search") != null ? request.getParameter("search") : "";
        int page = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 1;
        int pageSize = 5;

        try {
            if ("delete".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                taskDAO.deleteTask(id);
                response.sendRedirect("TaskServlet");
                return;
            }

            if ("edit".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                Task task = taskDAO.getTaskById(id);
                request.setAttribute("task", task);
                request.getRequestDispatcher("edittask.jsp").forward(request, response);
                return;
            }

            int totalCount = taskDAO.getTotalCount(search);
            int totalPages = (int) Math.ceil((double) totalCount / pageSize);
            List<Task> tasks = taskDAO.getTasks(search, page, pageSize);

            request.setAttribute("tasks", tasks);
            request.setAttribute("totalPages", totalPages);
            request.setAttribute("currentPage", page);
            request.getRequestDispatcher("taskmanage.jsp").forward(request, response);
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        try {
            if ("add".equals(action)) {
                int ordersid = Integer.parseInt(request.getParameter("ordersid"));
                String menuname = request.getParameter("menuname");
                int quantity = Integer.parseInt(request.getParameter("quantity"));
                int status = Integer.parseInt(request.getParameter("status"));

                Task task = new Task();
                task.setOrdersid(ordersid);
                task.setMenuname(menuname);
                task.setQuantity(quantity);
                task.setStatus(status);
                taskDAO.addTask(task);
            } else if ("edit".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                int ordersid = Integer.parseInt(request.getParameter("ordersid"));
                String menuname = request.getParameter("menuname");
                int quantity = Integer.parseInt(request.getParameter("quantity"));
                int status = Integer.parseInt(request.getParameter("status"));

                Task task = new Task();
                task.setId(id);
                task.setOrdersid(ordersid);
                task.setMenuname(menuname);
                task.setQuantity(quantity);
                task.setStatus(status);
                taskDAO.updateTask(task);
            }
            response.sendRedirect("TaskServlet");
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }
}
